import {ADD_TO_CART,UPDATE_CART,DELETE_FROM_CART} from '../actions/cart-action.js';
const initialState = {
  cart: [
    {
      product: 'bread 700g',
      quantity: 2,
      unitCost: 90
    },
    {
      product: 'milk 500ml',
      quantity: 1,
      unitCost: 47
    }
  ]
}
export default function(state = initialState, action){
	switch (action.type){
		case ADD_TO_CART:{
			return {
				...state,
				cart:[...state.cart,action.payload]
			}
		};
		case UPDATE_CART:{
			let cart = state.cart;
			cart.map(it=>{
				if(it.product === action.payload.product){
					it.quantity = action.payload.quantity;
					it.unitCost = action.payload.unitCost;
				}
			})
			return {
				...state,
				cart: cart
				// cart: state.cart.map(item => item.product === action.payload.product ? action.payload : item)	
			}
		}
		case DELETE_FROM_CART:{
			let cart = state.cart.filter(item => item.product !== action.payload.product)
			return {
				...state,
				cart:cart
			}
		}
		default:
			return  state;
	}
	
}